<?php

namespace App\Admin\Models;

use App\Admin\Models\SystemModel;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class WorshipAlbum extends Model
{
    use SoftDeletes;

    protected $guarded = []; 

    public function __construct(){
        parent::__construct();
        $this->correctionAlbum();
        $this->mergeCasts(SystemModel::fieldCasts($this->getTable()));
    }

    public function correctionAlbum(){
        DB::statement("SET SESSION group_concat_max_len = 1000000");
        DB::statement("UPDATE worship_albums AS wa LEFT JOIN ( SELECT album_id, CONCAT('[',GROUP_CONCAT('\"',hymn_id, '\"' ),']') AS hymns FROM worship_hymn_album GROUP BY album_id ) AS wha ON wa.id = wha.album_id SET wa.worship_hymns = wha.hymns");
    }
}
